package calorie.com.calorie.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;

import calorie.com.calorie.R;


public class LineChartHelper {
    LineChart lineChart;
    Context context;

    public LineChartHelper(LineChart lineChart, Context context) {
        this.lineChart = lineChart;
        this.context = context;
    }

    public  void initLineChart(String[] times, float max, float min, float per) {
        /**
         * 图表设置
         */
        //是否展示网格线
        lineChart.setDrawGridBackground(false);
        //显示边界
        lineChart.setDrawBorders(false);
        //是否可以拖动
        lineChart.setDragEnabled(true);
        //设置XY轴动画效果
        lineChart.animateX(2500);
        lineChart.animateY(1500);


        /**
         * X轴设置
         */
        XAxis xAxis = lineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //控制间隔
        xAxis.setGranularity(1f);
        xAxis.setLabelCount(times.length, false);
        xAxis.setGridColor(Color.GRAY);
        xAxis.setAxisLineColor(Color.GRAY);
        xAxis.setTextColor(Color.GRAY);
        xAxis.enableGridDashedLine(10,10,0);
        MyXFormatter formatter = new MyXFormatter(times);
        xAxis.setValueFormatter(formatter);

        /**
         *  Y轴设置
         */
        YAxis leftAxis = lineChart.getAxisLeft();
        leftAxis.setEnabled(false);
        YAxis rightAxis = lineChart.getAxisRight();
        rightAxis.setGranularity(per);
        rightAxis.setLabelCount(times.length, false);
        rightAxis.setGridColor(Color.GRAY);
        rightAxis.setTextColor(Color.GRAY);
        rightAxis.setAxisLineColor(Color.GRAY);
        rightAxis.setAxisMinimum((float) Math.floor(min));
        rightAxis.setAxisMaximum((float) (Math.floor(max)+1));
        rightAxis.enableGridDashedLine(10, 10, 0);

        ///图标的下边的指示块  图例
        Legend legend = lineChart.getLegend();
        legend.setEnabled(false);

        Description description = lineChart.getDescription();
        description.setEnabled(false);
    }


    /**
     * @Description: 显示图表
     * @Param: []
     * @return:
     */
    public void showLineChart(Float[]prop) {
        //设置数据
        List<Entry> entries = new ArrayList<>();
        for (int i = 0; i < prop.length; i++) {
            entries.add(new Entry(i,  prop[i]));
        }
        //一个LineDataSet就是一条线
        LineDataSet lineDataSet = new LineDataSet(entries, "价格");
        int color = Color.parseColor("#FF8416");
        lineDataSet.setColor(color);
       // lineDataSet.setCircleColor(color);
        lineDataSet.setLineWidth(1f);
        lineDataSet.setDrawCircles(false);
     //   lineDataSet.setCircleRadius(0f);

        //设置曲线值点空心
        lineDataSet.setDrawCircleHole(false);
        lineDataSet.setDrawValues(false);
        lineDataSet.setValueTextColor(Color.WHITE);

        //设置折线图填充
        lineDataSet.setFormLineWidth(50f);
        lineDataSet.setFormSize(15.f);
        lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        //设置渐变
        Drawable drawable = context.getResources().getDrawable(R.drawable.fade_chart);
        lineDataSet.setDrawFilled(true);
        lineDataSet.setFillDrawable(drawable);
        lineChart.invalidate();

        LineData data = new LineData(lineDataSet);
        lineChart.setData(data);
    }


    public class MyXFormatter  implements IAxisValueFormatter {

        private String[] mValues;

        public MyXFormatter(String[] values) {
            this.mValues = values;
        }

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            // "value" represents the position of the label on the axis (x or y)
            return mValues[(int) value % mValues.length]+"";
        }
    }

}
